<template>
    <n-drawer :show="active" :width="width" :mask-closable="false" @update:show="(flag:boolean) => (active = flag)">
        <n-drawer-content  :native-scrollbar="false" title="编辑人员信息" closable>
            <n-result status="418" title="暂未开放" description="此功能正在开发"></n-result>

            <!-- <n-form :model="model" :rules="rules" ref="formRef">
                <n-grid :cols="24" :x-gap="24">
                    <n-form-item-gi :span="14" label="员工名称" path="name">
                        <n-input placeholder="请填写员工名称" v-model:value="model.name" maxlength="32" show-count clearable />
                    </n-form-item-gi>
                    <n-form-item-gi :span="10" label="手机号" path="mob">
                        <n-input placeholder="请填写手机号" v-model:value="model.mob" maxlength="11" show-count clearable />
                    </n-form-item-gi>
                    <n-form-item-gi :span="24" label="员工角色" path="roles">
                        <n-checkbox-group v-model:value="model.roles">
                            <n-space>
                                <n-checkbox v-for="r in UserRoleOpts" :value="r.value" :label="r.label" />
                            </n-space>
                        </n-checkbox-group>
                    </n-form-item-gi>
                    <n-form-item-gi :span="24" label="负责区域 [不选择则表示全国区域]" path="areas">
                        <Distict v-model:code="model.areas" :max="5000"></Distict>
                    </n-form-item-gi>
                    <n-form-item-gi :span="12" label="绑定密码" path="pwd" v-if="!model.id">
                        <n-input placeholder="请填写绑定密码" v-model:value="model.pwd" maxlength="10" show-count clearable />
                    </n-form-item-gi>
                    <n-form-item-gi :span="12" label="有效时长（小时）" path="hour" v-if="!model.id">
                        <n-input-number v-model:value="model.hour" :min="1" :max="24 * 7" clearable />
                    </n-form-item-gi>
                </n-grid>
            </n-form> -->
            <template #footer>
                <n-space justify="end">
                    <n-button type="tertiary" :disabled="loading" @click="() => (active = false)">
                        <template #icon>
                            <n-icon :component="CloseRound" />
                        </template>
                        关闭
                    </n-button>
                    <!-- <n-button :disabled="loading" @click="execReset">
                        <template #icon>
                            <n-icon :component="RestoreRound" />
                        </template>
                        重置
                    </n-button>
                    <n-button type="primary" :loading="loading" @click="execSave">
                        <template #icon>
                            <n-icon :component="CheckRound" />
                        </template>
                        提交保存
                    </n-button> -->
                </n-space>
            </template>
        </n-drawer-content>
    </n-drawer>
</template>

<script lang="ts" setup>
import { computed, ref, onBeforeMount } from 'vue'
import { CloseRound } from '@vicons/material'

import { Http } from '@/libs'
import { useStore } from '@/store'

const store = useStore()
const width = computed(() => Math.max(store.state.browser.innerWidth * 0.4, 640))

const { $notification: notification } = window

const loading = ref(false)

const prop = defineProps({
    show: {
        type: Boolean,
        required: true,
    },
    item: {
        type: Object,
        default: {},
    },
})

const emit = defineEmits(['update:show', 'refresh'])

const active = computed({
    get: () => prop.show,
    set: val => emit('update:show', val),
})

const formRef = ref<any>(null)

const model = ref<any>({})

const rules = {
    roles: {
        type: 'array',
        required: true,
        trigger: 'change',
        message: '请选择员工角色',
    },
    name: {
        required: true,
        trigger: ['blur', 'input'],
        message: '请填写员工名称',
    },
    mob: {
        required: true,
        trigger: ['blur', 'input'],
        message: '请填写员工手机号',
    },
}

const execReset = () => {
    // if (!prop.item.id) {
    //     model.value = {
    //         id: '',
    //         roles: [],
    //         areas: [],
    //         name: '',
    //         mob: '',
    //         pwd: '',
    //         hour: 1,
    //     }
    //     return
    // }
    // model.value = {
    //     id: prop.item.id,
    //     roles: prop.item.roles,
    //     areas: prop.item.areas.map((x: any) => x.code),
    //     name: prop.item.name,
    //     mob: prop.item.mob,
    // }
}

const execSave = (e: Event) => {
    e.preventDefault()
    formRef.value.validate((errors: any[]) => {
        if (!errors) {
            loading.value = true
            Http.post('/ops/sys/emp/save', model.value).then(resp => {
                if (resp.stat.err > 0) {
                    loading.value = false
                    return
                }
                emit('refresh', {})
                notification.success({
                    duration: 1500,
                    title: '操作成功',
                    content: '您已成功保存员工信息',
                })
                setTimeout(() => {
                    loading.value = false
                    emit('update:show', false)
                }, 200)
            })
        } else {
            let msgs = errors.map(err => err[0].message)
            notification.error({
                duration: 1500,
                title: '请填写完整内容',
                description: '当前表单存在以下问题，请处理后提交',
                content: msgs.join('\n'),
            })
        }
    })
}

onBeforeMount(() => {
    execReset()
})
</script>
